//package com.hmdp.utils;/**
// * @Description: TODO
// * @Author: sats@jz
// * @Date: 2024/10/29 15:19
// **/
//
//import cn.hutool.core.lang.UUID;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.rabbit.connection.CorrelationData;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.util.concurrent.ListenableFutureCallback;
//
///**
// * @description TODO
// * @author sats@jz
// * @date 2024年10月29日 15:19
// */
//@Slf4j
//@RequiredArgsConstructor
//public class MqHelper {
//    private final RabbitTemplate rabbitTemplate;
//
//    public void sendMessage(String exchange, String routingKey, Object msg){
//        log.debug("准备发送消息，exchange:{}, routingKey:{}, msg:{}", exchange, routingKey, msg);
//        rabbitTemplate.convertAndSend(exchange, routingKey, msg);
//    }
//
//    public void sendDelayMessage(String exchange, String routingKey, Object msg, int delay){
//        rabbitTemplate.convertAndSend(exchange, routingKey, msg, message -> {
//            message.getMessageProperties().setDelay(delay);
//            return message;
//        });
//    }
//
//    public void sendMessageWithConfirm(String exchange, String routingKey, Object msg, int maxRetries){
//        log.debug("准备发送消息，exchange:{}, routingKey:{}, msg:{}", exchange, routingKey, msg);
//        CorrelationData cd = new CorrelationData(UUID.randomUUID().toString(true));
//        cd.getFuture().addCallback(new ListenableFutureCallback<>() {
//
//            int retryCount;
//            @Override
//            public void onFailure(Throwable ex) {
//                log.error("处理ack回执失败", ex);
//            }
//            @Override
//            public void onSuccess(CorrelationData.Confirm result) {
//                if (result != null && !result.isAck()) {
//                    log.debug("消息发送失败，收到nack，已重试次数：{}", retryCount);
//                    if(retryCount >= maxRetries){
//                        log.error("消息发送重试次数耗尽，发送失败");
//                        return;
//                    }
//                    CorrelationData cd = new CorrelationData(UUID.randomUUID().toString(true));
//                    cd.getFuture().addCallback(this);
//                    rabbitTemplate.convertAndSend(exchange, routingKey, msg, cd);
//                    retryCount++;
//                }
//            }
//        });
//        rabbitTemplate.convertAndSend(exchange, routingKey, msg, cd);
//    }
//}
